基于OpenCV的USB摄像头 采集显示与保存.CPP #include <cv.h>
#include <highgui.h>
#include <stdio.h>
//实现将拍下来的图片缩放一倍
IplImage * DoPyrDown(IplImage * image,int filter = IPL_GAUSSIAN_5x5)
{
//设置大小缩小一倍
CvSize size = cvSize(image->width / 2,image -> height / 2);
//初始化图片
IplImage * outImage = cvCreateImage(size,image->depth,image->nChannels);
//实现缩放
cvPyrDown(image,outImage);
cvReleaseImage(&image);
return outImage;
}
int main(int argc,char ** argv)
{
cvNamedWindow("Demo08");
CvCapture * capture;
//首先是要通过摄像设备来得到一个CvCapture 对象
if(argc == 1)
{
capture=cvCreateCameraCapture(0);
}
else
{
capture=cvCreateCameraCapture(atoi(argv[1]));
}
assert(capture!=NULL);
IplImage * frame;
char keyCode;
//每隔30ms 捕捉一次视频图像
while((keyCode = cvWaitKey(30)))
{
//表示按下了Esc 键
if(keyCode == 27)
{
break;
}
//表示按下了回车键,此时应当保存照片
if(keyCode == 13)
{
//初始化一张图片
IplImage * outImage = cvCreateImage(
cvGetSize(frame),
frame->depth,
frame->nChannels);
//将原图拷贝过来
cvCopy(frame,outImage,NULL);
//实现缩放操作
outImage= DoPyrDown(outImage);
char *outImageName = "XiaoZhen.jpg";
//将图片保存
cvSaveImage(outImageName,outImage);
cvReleaseImage(&outImage);
printf("恭喜你, 保存图片成功! \n");
}
//得到摄像设备中的下一帧图像
frame = cvQueryFrame(capture);
if(!frame)
{
break;
}
cvShowImage("Demo08",frame);
}
cvReleaseImage(&frame);
cvDestroyAllWindows();
return 0;
}.
└── 找例子网_opencv_usbcam.cpp
0 directories, 1 file
评论